package bsc.war.handler;

import bsc.ejb.entity.Comparator;
import bsc.ejb.entity.DimensionMember;
import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Milestone;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.Project;
import bsc.ejb.entity.Tracer;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.io.OutputStream;
import java.lang.Long;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Memo
 */
public class ProjectTemplate extends AbstractController {

    public ProjectTemplate() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            processRequestInternal(request, response, delegate);

        } catch (Exception e) {
            logger.error("Catched exception", e);
        }
        return null;
    }

    public static void processRequestInternal(HttpServletRequest request,
            HttpServletResponse response, DelegateBSC delegate) {


        try {

            response.setContentType("application/vnd.ms-excel"); //Tipo de fichero.
            response.setHeader("Content-Disposition", "attachment;filename=\"templateProject.xls\"");

            //Find the scorecard
            Long idProject = Long.parseLong(request.getParameter("scoreCard"));
            Project project = (Project) delegate.findScoreCard(idProject);

            //List of Milestones
            List <Milestone> milestones = delegate.findMilestone(project);

            //List of Tracers
            List <Tracer> tracers = delegate.findTracer(project);

            //List of Type of data
            String dataType []= {"Real", "Meta"};

            //Create Excel
            HSSFWorkbook wb = new HSSFWorkbook();

            for(int i = 0; i <dataType.length; i++){
                HSSFSheet sheetDataType = wb.createSheet("" + dataType[i]);

                //PROJECT ROW
                HSSFRow rowProject = sheetDataType.createRow(0);
                rowProject.createCell(0).setCellValue("Proyecto");
                //ID
                rowProject.createCell(1).setCellValue(project.getIdScoreCard());
                //Name
                rowProject.createCell(2).setCellValue(project.getName());

                //ROW HITOS TITTLE
                HSSFRow rowMilestoneTittle = sheetDataType.createRow(2);
                rowMilestoneTittle.createCell(1).setCellValue("HITOS");

                //ROW HITOS
                HSSFRow rowMilestones = sheetDataType.createRow(3);
                rowMilestones.createCell(0).setCellValue("Indicadores");
                for (int j = 0; j < milestones.size(); j++) {
                    Milestone milestone = milestones.get(j);
                    rowMilestones.createCell(j+1).setCellValue(milestone.getIdTask()+ "-" + milestone.getName());
                }

                //ADD TRACER
                int countTracer = 4;
                for(int j = 0; j < tracers.size(); j++){
                    Tracer tracer = tracers.get(j);
                    //System.out.println("DataType: " + dataType[i]);
                    //System.out.println("Tracer Type: " + tracer.getType());
                    if(!(dataType[i].equals("Meta")&& tracer.getType()==Tracer.Type.PROGRESS)){
                       HSSFRow rowTracer = sheetDataType.createRow(countTracer);
                       rowTracer.createCell(0).setCellValue(tracer.getIdTracer()+"-"+tracer.getName());
                       countTracer++;
                       //System.out.println("LO ACEPTE");
                    }
                }
            }

            OutputStream output = response.getOutputStream();

            wb.write(output);
            output.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
